import $ from 'jquery';
import bus from '@/js/common/bus.js';
import httptool from './zry.plugins.httptool.js';
import tool from '@/js/plugins/zry.plugins.tool.js';
export default {
	cloneObj(obj) { //复制对象
		if(typeof obj == 'string') {
			obj = JSON.parse(obj)
		}
		var objDeepCopy = function(source) {
			var sourceCopy = source instanceof Array ? [] : {};
			for(var item in source) {
				sourceCopy[item] = (source[item] && typeof source[item] === 'object') ? objDeepCopy(source[item]) : source[item];
			}
			return sourceCopy;
		}
		return objDeepCopy(obj);
	},
	cloneArr(obj) {
		var out = [],
			i = 0,
			len = obj.length;
		for(i == 0; i < len; i++) {
			if(obj[i] instanceof Array) {
				out[i] = this.cloneArr(obj[i]);
			} else {
				out[i] = obj[i]
			};
		}
		return out;
	},
	changeHtmlTitle(title) {
		$('title').text(title);
	},
	GetQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
		var context = "";
		if(r != null)
			context = r[2];
		reg = null;
		r = null;
		return context == null || context == "" || context == "undefined" ? "" : context;

	},
	getTargetDom(dom, targetClass) {
		var tag = 0
		while(!dom.hasClass(targetClass)) {
			tag++
			if(tag > 100) {
				return false;
			}
			dom = dom.parent();
		}
		return dom;
	},
	preview(ev, id = null) {
		if(id) {
			let src = $(ev.target).attr('src');
			let html = `<div class="imgPreview my_flex_center" >
							<div>
								<img src="${src}" alt="" />
							</div>
						</div>`
			$('body').append(html);

			$(".imgPreview").unbind().click(e => {
				let dom = this.getTargetDom($(e.target), 'imgPreview');
				let isPreview = dom.hasClass('imgPreview')
				if(isPreview) {
					dom.remove()
				}
			})
		}

	},
	getImgUrlArr(urls, arr) { //根据拼接的url往图片墙数组中添加数据
		if(urls) {
			var strArr = urls.split(',');
			for(var i in strArr) {
				arr.push(strArr[i]);
			}
			console.log(arr);
		}
	},
	setStorage(key_, val) {
		if(typeof val == 'object') {
			val = JSON.stringify(val)
		}
		sessionStorage.setItem(key_, val)
	},
	isLogin(self){
		if(!self.$store.state.userInfo){
			self.$router.push('/login');
			return false;
		}
		return true;
	},
	getDirByKey(self, arr) {
		return new Promise((resolve, reject) => {
			let params = {
				categorys: arr,
				pageNum: 1,
				pageSize: 100
			};
			httptool.ajax2('dict/getListValue', params, "POST", true, res => {
				res.arr_key = arr;
				bus.$emit('changeValue', res);
				resolve();
			});
			return;
			let arr_key = [];

			for(let key_ of arr) {
				let obj = self.$store.state[key_];
				if(!obj) {
					obj = {};
				}
				if(Object.keys(obj).length == 0) {
					arr_key.push(key_);
				}
			}
			if(arr_key.length == 0) {
				resolve();
			} else {
				let params = {
					categorys: arr_key,
					pageNum: 1,
					pageSize: 100
				};
				httptool.ajax2('dict/getListValue', {}, "POST", true, res => {
					res.arr_key = arr_key;
					bus.$emit('changeValue', res);
					resolve();
				});
			}
		})
	},
	webkitExecute(param, callback) {
		if(window.android != null && typeof(window.android) != "undefined") {
			window.android.callAndroid(JSON.stringify(param));
		} else if(window.webkit != null && typeof(window.webkit) != "undefined" &&
			window.webkit.messageHandlers != null && window.webkit.messageHandlers != undefined &&
			window.webkit.messageHandlers.senderModel != null && window.webkit.messageHandlers.senderModel != undefined) {
			window.webkit.messageHandlers.senderModel.postMessage(JSON.stringify(param));
		} else {
			callback();
		}
	},
	confirmToken(self){
		httptool.ajax2('customer/checkToken',{}, "POST", true, res => {
			self.$store.commit('changeValue', {
				name: 'validToken',
				value: res.data
			});
			return res.data;
		});
	},
	cleanToken(){
		sessionStorage.removeItem('userId');					
		sessionStorage.removeItem('token');
		localStorage.removeItem('token');
	},
	setToken(token){
		localStorage.setItem('token', token);
		sessionStorage.setItem('token', token);
	},
	getToken(){
		if(sessionStorage.getItem("token")){
			return sessionStorage.getItem("token");
		}
		return localStorage.getItem("token");
	},
	trimNull(obj, defaultobj){
		if(obj == null || obj == ''){
			obj = defaultobj;
		}
		return obj;
	}
}